package cn.zftt.quartz.service.impl;

import cn.zftt.quartz.dao.JobDao;
import cn.zftt.quartz.dao.JobLogDao;
import cn.zftt.quartz.model.Job;
import cn.zftt.quartz.model.JobLog;
import cn.zftt.quartz.model.Page;
import cn.zftt.quartz.service.JobService;
import org.apache.commons.lang3.StringUtils;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zhangfeng
 */
@Service
@Transactional
public class JobServiceImpl implements JobService {

    private final Scheduler scheduler;

    private final JobDao jobDao;

    private final JobLogDao jobLogDao;

    @Override

    public Page<Job> find(Job job) throws SchedulerException {
        if (StringUtils.isNotEmpty(job.getName()))
            return jobDao.queryJobs(job);
        else
            return jobDao.queryAllJobs(job);
    }

    @Override
    public void add(Job job) throws ClassNotFoundException, SchedulerException {
        if (!checkJob(job))
            throw new IllegalArgumentException("参数出错");
        Class clazz = Class.forName(job.getClassName().trim());
        JobDetail jobDetail = JobBuilder.newJob(clazz).withIdentity(job.getName(), job.getGroup()).withDescription(job.getDescription()).build();
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression()).withMisfireHandlingInstructionDoNothing();
        Trigger trigger = TriggerBuilder.newTrigger().forJob(jobDetail).startNow().withIdentity("T_" + job.getName(), "T_" + job.getGroup()).withSchedule(scheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, trigger);
    }

    @Override
    public void remove(Job job) throws SchedulerException {
        if (!checkJob(job))
            throw new IllegalArgumentException("参数错误");
        JobKey jobKey = JobKey.jobKey(job.getName(), job.getGroup());
        scheduler.deleteJob(jobKey);
    }

    @Override
    public void runOnce(Job job) throws SchedulerException {
        if (!checkJob(job))
            throw new IllegalArgumentException("参数错误");
        JobKey jobKey = JobKey.jobKey(job.getName(), job.getGroup());
        scheduler.triggerJob(jobKey);
    }

    @Override
    public void pause(Job job) throws SchedulerException {
        if (!checkJob(job))
            throw new IllegalArgumentException("参数错误");
        JobKey jobKey = JobKey.jobKey(job.getName(), job.getGroup());
        scheduler.pauseJob(jobKey);
    }

    @Override
    public void resume(Job job) throws SchedulerException {
        if (!checkJob(job))
            throw new IllegalArgumentException("参数错误");
        JobKey jobKey = JobKey.jobKey(job.getName(), job.getGroup());
        scheduler.resumeJob(jobKey);
    }

    @Override
    public void reschedule(Job job) throws SchedulerException {
        if (!checkJob(job))
            throw new IllegalArgumentException("参数错误");
        TriggerKey triggerKey = TriggerKey.triggerKey("T_" + job.getName(), "T_" + job.getGroup());
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression()).withMisfireHandlingInstructionDoNothing();
        Trigger trigger = TriggerBuilder.newTrigger().startNow().withIdentity("T_" + job.getName(), "T_" + job.getGroup()).withSchedule(scheduleBuilder).build();
        scheduler.rescheduleJob(triggerKey, trigger);
    }

    @Override
    public Page logs(JobLog jobLog) {
        Page page = jobLogDao.getLogs(jobLog);
        return page;
    }


    @Autowired
    public JobServiceImpl(Scheduler scheduler, JobDao jobDao, JobLogDao jobLogDao) {
        this.scheduler = scheduler;
        this.jobDao = jobDao;
        this.jobLogDao = jobLogDao;
    }


    private boolean checkJob(Job job) {
        if (job != null)
            if (StringUtils.isNotEmpty(job.getGroup()) && StringUtils.isNotEmpty(job.getName()))
                return true;
        return false;
    }
}
